{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6a9cc614-7171-47fd-affd-220773a7066c",
   "metadata": {},
   "source": [
    "## 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d626f682-7356-4b1d-a510-3d8931f2d1ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "def add(a, b):\n",
    "    r = a + b\n",
    "    return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0e994c6e-10dd-4e62-bb61-5e95e861a6e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b0ab20c-d395-401a-ba3b-e70d7c7836da",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "72987719-8e07-4085-9aa4-c83d4d1983db",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_growth_rate(old_value, new_value):\n",
    "    \n",
    "    growth_rate = (new_value - old_value) / old_value\n",
    "    \n",
    "    return growth_rate * 100\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8a347d1c-d136-4e14-b385-2d0442c28827",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calculate_growth_rate(10, 15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "848f9f47-d792-4fb4-9a5e-c8f281d2b0dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-25.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calculate_growth_rate(20, 15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36ce78da-5fc1-409a-88ff-3a15274a98d0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6819c021-7e4e-461e-a666-bdff08783e93",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(a, b, c):\n",
    "    print(f'a={a}, b={b}, c={c}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57172ad9-d8e1-44f8-875f-cae8fa2cdb2a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "312f900c-6402-4c28-9b11-3dddee9ed76e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=10, b=20, c=30\n"
     ]
    }
   ],
   "source": [
    "f(10, 20, 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "306ec448-fbc9-4b7f-aa71-e092c4cac3bd",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "f() takes 3 positional arguments but 4 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[12], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m30\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m40\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: f() takes 3 positional arguments but 4 were given"
     ]
    }
   ],
   "source": [
    "f(10, 20, 30, 40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "802df5a9-2372-4615-b938-36750eed51dd",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "f() missing 1 required positional argument: 'c'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[13], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: f() missing 1 required positional argument: 'c'"
     ]
    }
   ],
   "source": [
    "f(10, 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df8ef99e-bb6a-4503-b118-b80432e5b865",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "23c1671e-f44b-4f16-b59b-272b90f5fd12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=10, b=20, c=30\n"
     ]
    }
   ],
   "source": [
    "f(a=10, b=20, c=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "545a7a14-7ec8-4312-9037-ced46947b4cb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "46658bf6-b73c-46b7-81f2-49f07e640ec0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=10, b=20, c=30\n"
     ]
    }
   ],
   "source": [
    "f(c=30, a=10, b=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f19ab0a8-94e6-4380-a6ec-c0a5bd2cbbd9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ac45e72a-d943-456e-8720-eff7ad6cdc6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=10, b=20, c=30\n"
     ]
    }
   ],
   "source": [
    "f(10, c=30, b=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6006aa95-79a2-4963-bf3e-1c1ca04cd66e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "abb514a1-509e-4275-9c9b-ad652750f7eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=10, b=20, c=30\n"
     ]
    }
   ],
   "source": [
    "f(10, 20, c=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f032d67-d0ae-4c30-84a5-6817aea1169e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a424b817-e93e-45d5-b33b-c8e639f37cb7",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (2955790422.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[18], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m    f(a=10, b=20, 30)\u001b[0m\n\u001b[0m                    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(a=10, b=20, 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b74efc19-7dc1-4928-879d-3dfb4dd4c59a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4a17cdb1-cf0d-414d-95ea-382125165568",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (1539846992.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[19], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m    f(b=20, 10, 30)\u001b[0m\n\u001b[0m                  ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(b=20, 10, 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a83ccd4-571c-4b28-bd56-1429aa0161c6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8a50752f-6124-4d60-9349-cca905f3a5f0",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "f() got multiple values for argument 'a'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[21], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m30\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: f() got multiple values for argument 'a'"
     ]
    }
   ],
   "source": [
    "f(10, c=30, a=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b31a80b0-9636-4ead-955a-59eb826f61be",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b0539a0c-d6f6-4ff4-ba3b-6ad038b0fa04",
   "metadata": {},
   "source": [
    "## 参数默认值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c6ef913-7818-4f4b-af2a-736dfabb2d02",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00525e8c-40e0-4694-9e34-5cc128824e37",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ea09fb0b-1655-4513-9892-3b2323a1b0b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您好, 张三!\n"
     ]
    }
   ],
   "source": [
    "def greet(greeting, name):\n",
    "    print(f\"{greeting}, {name}!\")\n",
    "\n",
    "greet('您好', '张三')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cfa997f-d521-4ae7-a2cd-a91d855fc7ba",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d489d44c-c301-4178-b823-6a8659536afc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您好, 张三!\n"
     ]
    }
   ],
   "source": [
    "greet('您好', '张三')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2edd1c2f-ddfc-48c7-8b25-a5eda676e960",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好, 张三!\n"
     ]
    }
   ],
   "source": [
    "greet('你好', '张三')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80543d7c-a798-49bb-90ee-103b2af83145",
   "metadata": {},
   "outputs": [],
   "source": [
    "greet('张三')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddb5c49c-129a-4a78-bc35-b65d833d2e65",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "069135a9-099e-4eaa-99ab-cd398b998b09",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet(name, greeting=\"你好\"):\n",
    "    print(f\"{greeting}, {name}!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1ce7641-b76f-4717-8b00-1e4a54e29e24",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a7edd0ca-4600-4a56-88b7-9bfa1bfe78f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好, 张三!\n"
     ]
    }
   ],
   "source": [
    "greet('张三')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af0ea0b0-032d-4fdd-95f4-e86c15733f1b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9961daf4-2ca5-455f-b704-43a1a237c0e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi, 张三!\n"
     ]
    }
   ],
   "source": [
    "greet('张三', 'Hi')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77bae31b-c628-4e89-bf75-7809b703481a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "312556f9-b00d-4627-a07d-d2d83442b5f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi, 张三!\n"
     ]
    }
   ],
   "source": [
    "greet('张三', greeting='Hi')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80f5917c-b3e7-4e8f-8f96-7acbc29454e5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "8bd87949-b2e2-4b75-b14f-dd15d4e7d8d8",
   "metadata": {},
   "source": [
    "## 变量作用域"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5d539c42-0693-4174-847a-3f8bc824effc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f: x=100\n",
      "x=15\n"
     ]
    }
   ],
   "source": [
    "def f():\n",
    "    x = 100\n",
    "    print(f\"f: x={x}\")\n",
    "\n",
    "x = 15\n",
    "\n",
    "f()\n",
    "print(f\"x={x}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90fa6687-771e-422f-b0d0-8ca71b89bd81",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "04d365dd-6683-4a16-8feb-a8c7922e9f86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f: x=100\n",
      "x=100\n"
     ]
    }
   ],
   "source": [
    "def f():\n",
    "    global x\n",
    "    x = 100\n",
    "    print(f\"f: x={x}\")\n",
    "\n",
    "x = 15\n",
    "\n",
    "f()\n",
    "print(f\"x={x}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "893d01d4-cd8d-4a74-ba79-ee56eab6db15",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0154901d-8123-4293-a1be-b547e8072ab7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f: 1\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "def f():\n",
    "    x = 1\n",
    "    print(\"f:\", x)\n",
    "    \n",
    "x = 11\n",
    "\n",
    "f()\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb209b7e-bb60-4a5a-a90b-fc3b2537ac03",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "9da07f1b-dc85-4763-9bc9-aa6ba68742d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A: x=10, y=20\n",
      "F: x=10, y=25, z=99\n",
      "B: x=10, y=20\n"
     ]
    }
   ],
   "source": [
    "x = 10\n",
    "y = 20\n",
    "\n",
    "def f(z):\n",
    "    y = 25\n",
    "    print(f'F: x={x}, y={y}, z={z}')\n",
    "\n",
    "print(f'A: x={x}, y={y}')\n",
    "f(99)\n",
    "print(f'B: x={x}, y={y}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "319ce769-6b8f-4e42-8c0e-795e3715bb16",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
